package Dao;

import entity.Goods;
import entity.Order;
import entity.User;
import util.DatabaseUtil;

import javax.servlet.annotation.WebServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl extends BasDao implements OrderDao {

    public OrderDaoImpl(Connection conn){
        super(conn);
    }


    /*public List<Order> queryOrder(int uid, Page page){
        ResultSet rs = null;
        Order order =null;
        List<Order> list = new ArrayList<>();
        PreparedStatement pstmt = null;
        String sql = "SELECT * FROM `order` WHERE user_id = ? ";

        try {
            if (page != null){
                sql += " LIMIT ?,?";
                rs =  super.executeQuery(sql,uid,(page.getCurrPageNo()-1)*5,page.getPageSize());
            }else {
                rs =  super.executeQuery(sql,uid);
            }
            while (rs.next()){
                order = new Order();
                order.setId(rs.getInt("id"));
                order.setName(rs.getString("name"));
                order.setAddress(rs.getString("address"));
                order.setTotal(rs.getInt("total"));
                order.setAmount(rs.getInt("amount"));
                order.setStatus(rs.getInt("status"));
                order.setQuantity(rs.getInt("quantity"));
                order.setPaytype(rs.getInt("paytype"));
                order.setPhone(rs.getString("phone"));
                order.setDatetime(rs.getString("datetime"));
                order.setUserId(rs.getInt("user_id"));
                list.add(order);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DatabaseUtil.closeAll(null, pstmt, rs);
        }
        return list;
    }*/

    @Override
    public boolean addOrder(User user, Goods goods, int amount, int status, int paytype){
        String sql = "INSERT INTO `order` (total,amount,`status`,paytype,`name`,phone,address,datetime,user_id,quantity) VALUES(?,?,?,？,?,?,?,NOW(),?,?)";
        try {
            int num = super.executeUpdate(sql,(goods.getPrice()*amount),amount,status,paytype,user.getName(),user.getPhone(),user.getAddress(),user.getId(),goods.getId());
            if (num > 0) return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean dltOrder(int gid){
        String sql = "DELETE FROM `order` WHERE id = ?";

        try {
            int num = super.executeUpdate(sql,gid);
            if (num > 0) return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
